package chapter1.service.array;

import chapter1.domain.entity.Role;
import chapter1.domain.entity.dto.RoleDto;
import chapter1.domain.entity.vo.RoleVo;
import chapter1.domain.entity.vo.Result;
import chapter1.service.IRoleService;
import chapter1.service.IUserService;
import chapter1.util.SingletonFactory;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 业务层-角色
 *
 * @author 郑植航
 * @since 2025/4/18 15:20
 */
@Deprecated
public class RoleService extends BaseService<Role,RoleDto,RoleVo>implements IRoleService {


    private IUserService userService;


    private RoleService(){
        data = new Role[DATA_SIZE+1];
        temp = new RoleVo[DATA_SIZE];
    }



    @Override
    RoleVo convertToVo(Role role){
        return new RoleVo(role.getId(),role.createAt,role.updateAt,role.name,role.dataScope);
    }

    @Override
    Role createEntity(RoleDto dto,int id){
        LocalDateTime now = LocalDateTime.now();
        return new Role(id,now,now,dto.name, dto.dataScope);
    }
    @Override
    void updateEntity(RoleDto dto,Role role){
        role.updateAt=LocalDateTime.now();
        role.name=dto.name;
        role.dataScope=dto.dataScope;
    }


    @Override
    public  Result delete(int id) {
        if (!existById(id)) {
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService==null){
            userService = SingletonFactory.getInstance(UserService.class);
        }
        if(userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data[id]=null;
        return Result.success("删除成功", null);
    }
}
